<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { defaultFormData, formRules } from '../index'
import type { CustomDialogInstance, DepartmentalInfo } from '@/types'
import { queryTreeList } from '@/api/modules/sysDepartmental'

// ------------------use------------------
// #region
defineProps<{
  title: string
}>()
const emits = defineEmits<{
  'save-success': []
}>()
const formData = defineModel('data', {
  default: () => defaultFormData,
})
const { vLoading, withLoading } = useDefineLoading({
  destroyNode: true,
})
const [departmentData, setDepartmentData] = useQuoteState<
  DepartmentalInfo[]
>([])
// #endregion
// ------------------静态变量------------------
// #region

// #endregion
// ------------------动态变量------------------
// #region

// #endregion
// ------------------ref-----------
// #region
const dialogRef = ref<CustomDialogInstance | null>(null)
const formRef = ref<FormInstance | null>(null)
// #endregion
// ------------------reactive------
// #region
// #endregion
// ------------------computed------
// #region

// #endregion
// ------------------生命周期------------------
// #region
// #endregion
// ------------------内置方法---------------------
// #region
// #endregion
// ------------------方法---------------------
// #region
async function getDepartmentData() {
  const { data: res } = await withLoading(queryTreeList)()
  console.log(res)

  setDepartmentData(res)
}
async function open() {
  formRef.value?.resetFields()
  await nextTick()
  dialogRef.value?.open?.()
  getDepartmentData()
}
function close() {
  dialogRef.value?.close?.()
}
async function clickSave(done: () => void) {
  try {
    await formRef.value?.validate()
    emits('save-success')
  }
  catch (error) {
    done()
  }
}
// #endregion
defineExpose({
  open,
  close,
})
</script>

<template>
  <CustomDialog
    ref="dialogRef"
    :title="title"
    width="600px"
    @confirm="clickSave"
  >
    <el-form
      ref="formRef"
      v-loading
      label-width="100px"
      label-position="right"
      :model="formData"
      :rules="formRules"
      inline
    >
      <el-form-item
        label="所属部门"
        prop="parentId"
        class="width-fill"
      >
        <el-cascader
          v-model="formData.parentId"
          placeholder="请选择部门"
          :options="departmentData"
          :props="{
            label: 'departName',
            value: 'id',
            emitPath: false,
            checkStrictly: true,
          }"
          filterable
          class="width-fill"
        />
      </el-form-item>
      <el-form-item
        label="部门"
        prop="departName"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.departName"
          maxlength="20"
          placeholder="请输入名称"
        />
      </el-form-item>
      <!-- <el-form-item label="负责人" class="width-fill">
        <el-input placeholder="请输入名称" />
      </el-form-item> -->
    </el-form>
  </CustomDialog>
</template>

<style lang="scss" scoped>
//------------------组件样式------------------
//-------------样式穿透-------------
</style>
